package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.exception.LogicException;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
@Topic(TopicConfig.VMS_STATUS_TOPIC)
public class AutoTaskHandler  implements MsgHandler {
    @Resource
    private TaskService taskService;

    @Override
    public void process(String jsonMsg) throws IOException {
        if(StringUtils.isEmpty(jsonMsg)){
            throw new LogicException("自动创建维修工单失败");
        }
        //解析协议  售货机状态
        VmStatusContract contract = JsonUtil.getByJson(jsonMsg, VmStatusContract.class);
        List<StatusInfo> statusInfo = contract.getStatusInfo();
        String desc="";
        for (StatusInfo info : statusInfo) {
            if(!info.isStatus()){
                //状态异常
                throw new LogicException("售货机状态异常");
            }
            if(StringUtils.isNotEmpty(desc)){
                TaskViewModel model =new TaskViewModel();
                //售货机编号
                model.setInnerCode(contract.getInnerCode());
                //设置工单创建方式  自动创建
                model.setCreateType(VMSystem.AUTO_CREATE_TASK);
                //故障信息
                model.setDesc(desc);
                //工单类型   维修
                model.setProductType(VMSystem.TASK_TYPE_REPAIR);
                //调用自动创建方法  自动创建维修工单
                taskService.updateTaskZSet(model,0,VMSystem.USER_REPAIRER);
            }
        }

    }
}
